
module MiniMagick
  class Image
    def compressing(convert_gif)
      format = self[:format]
      if convert_gif && format == "GIF"
        #convert 1st frame for animation gif
        run_command("convert","#{@path}[0]", "#{@path}")
      elsif format == "BMP"
        format("PNG")
      end
    end
  end
end

module CarrierWave

  module MiniMagick

    def manipulate!
      cache_stored_file! if !cached?
      image = ::MiniMagick::Image.open(current_path)

      image.compressing(true)
      begin
        image.format(@format.to_s.downcase) if @format
        image = yield(image)
        image.write(current_path)
        image.run_command("identify", current_path)
      ensure
        image.destroy!
      end
    rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
      default = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :locale => :en)
      message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :default => default)
      raise CarrierWave::ProcessingError, message
    end

  end


end

